import BoardUtils from "./board-utils";
import {BOARD, GAME_OVER_TYPE} from "../configs/config";

const boardUtils = new BoardUtils();

class CheckWin {

    /**
     * 判断游戏是否胜利
     * @param userId
     * @param gameMap
     * @param to
     */
    judgeGameWin = async (userId: string, gameMap: any, to: any) => {
        const log = global.logUtils.createContext('CheckWin', 'judge', {userId});
        // 棋盘数组二维化
        const board = boardUtils.listToArray(gameMap);
        // 计步变量和胜利要累计的个数变量
        let count = 1;
        const winCount = 5;
        // 结束坐标
        const {x, y, isBlackColor} = to;

        for (let i = x - 1; i >= 0; --i) {
            const chess = board[i][y];
            if (chess && chess.isBlackColor === isBlackColor) {
                count++;
            } else {
                break;
            }
        }

        for (let i = x + 1; i < BOARD.ROW_SIZE; ++i) {
            const chess = board[i][y];
            if (chess && chess.isBlackColor === isBlackColor) {
                count++;
            } else {
                break;
            }
        }
        if (count >= winCount) {
            return true;
        } else {
            count = 1;
        }

        for (let i = y - 1; y >= 0; --i) {
            const chess = board[x][i];
            if (chess && chess.isBlackColor === isBlackColor) {
                count++;
            } else {
                break;
            }
        }
        for (let i = y + 1; y < BOARD.COL_SIZE; ++i) {
            const chess = board[x][i];
            if (chess && chess.isBlackColor === isBlackColor) {
                count++;
            } else {
                break;
            }
        }

        if (count >= winCount) {
            return true;
        } else {
            count = 1;
        }

        for (let i = x + 1, j = y + 1; i < BOARD.ROW_SIZE && j < BOARD.COL_SIZE; ++i, ++j) {
            const chess = board[i][j];
            if (chess && chess.isBlackColor === isBlackColor) {
                count++;
            } else {
                break;
            }
        }
        // ����
        for (let i = x - 1, j = y - 1; i >= 0 && j >= 0; --i, --j) {
            const chess = board[i][j];
            if (chess && chess.isBlackColor === isBlackColor) {
                count++;
            } else {
                break;
            }
        }

        if (count >= winCount) {
            return true;
        } else {
            count = 1;
        }

        for (let i = x + 1, j = y - 1; i < BOARD.ROW_SIZE && j >= 0; ++i, --j) {
            const chess = board[i][j];
            if (chess && chess.isBlackColor === isBlackColor) {
                count++;
            } else {
                break;
            }
        }
        for (let i = x - 1, j = y + 1; i >= 0 && j < BOARD.COL_SIZE; --i, ++j) {
            const chess = board[i][j];
            if (chess && chess.isBlackColor === isBlackColor) {
                count++;
            } else {
                break;
            }
        }
        return count >= winCount;
    }
}

export default CheckWin;
